/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                       *
 * This program is free software; you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * Copyright (C) 2003-2007 e-Evolution,SC. All Rights Reserved.               *
 * Contributor(s): Victor Perez www.e-evolution.com                           *
 *****************************************************************************/

package org.compiere.process;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.logging.Level;

import org.compiere.model.MClient;
import org.compiere.util.DB;

/**
 * Production of BOMs
 *   1) Creating ProductionLines when IsCreated = 'N'
 *   2) Posting the Lines (optionally only when fully stocked)
 * 
 * @author victor.perez@e-evolution.com
 * @contributor: Carlos Ruiz (globalqss) - review backward compatibility - implement mustBeStocked properly
 */
public class FactAcctRepost extends SvrProcess {

	/** The Record */
	private int p_Record_ID = 0;

	/**	Period Parameter				*/
	private Timestamp			p_DateAcct_From = null;
	private Timestamp			p_DateAcct_To = null;
	/**	Org Parameter					*/
	private int					p_AD_Org_ID = 0;

	
	/**
	 * Prepare - e.g., get Parameters.
	 */
	protected void prepare() {
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null)
				;
			else if (name.equals("AD_Org_ID"))
			{
				p_AD_Org_ID = para[i].getParameterAsInt();
			}	
			else if (name.equals("DateAcct"))
			{
				p_DateAcct_From = (Timestamp)para[i].getParameter();
				p_DateAcct_To = (Timestamp)para[i].getParameter_To();
			}
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}
		p_Record_ID = getRecord_ID();
		log.log(Level.INFO, "Record_ID " + p_Record_ID);
		
	} //prepare

	/**
	 * Process
	 * 
	 * @return message
	 * @throws Exception
	 */

	protected String doIt() throws Exception 
	{
		log.info("Search fields in Fact_acct");
		int iTableID, iRecordID;
		String clientCheck = " AND AD_Client_ID=" + getAD_Client_ID();		
		StringBuffer sql =  new StringBuffer("SELECT AD_Table_ID, Record_ID "
    		+ " FROM Fact_Acct "
    		+ " WHERE isActive = 'Y'"
    		+ " AND DateAcct >="+DB.TO_DATE(p_DateAcct_From, true)
    		+ " AND DateAcct <="+DB.TO_DATE(p_DateAcct_To, true)
    		+ " AND AD_Org_ID = "+p_AD_Org_ID
    		+ clientCheck
    		+ " GROUP BY AD_Table_ID, Record_ID "
    		+ " ORDER BY AD_Table_ID, Record_ID "); 
		PreparedStatement pstmt = DB.prepareStatement (sql.toString(),null);
		ResultSet rs = null;
		try {				
		  rs = pstmt.executeQuery();
		  //iAccumDepAcct=iDepAcct=0;
		  while (rs.next()){
			  iTableID = rs.getInt("AD_Table_ID");
			  iRecordID = rs.getInt("Record_ID");
				 if ((iTableID>0)&&(iRecordID>0)) {
					 String ignoreError = DocumentEngine.postImmediate(getCtx(), getAD_Client_ID(), iTableID, iRecordID, true, get_TrxName());
					 System.out.println("Table_ID = "+iTableID+", Record_ID = "+iRecordID+" " +ignoreError);
				 }
		  }
		} catch (Exception e) {
			log.log(Level.SEVERE, "Error: " + e);
		} finally {
			DB.close(rs, pstmt);
		    rs = null; pstmt = null;
		}
		return "@OK@";
	} 

}
